import ApiService from '@/services/api.service'

class DocumentService {
  constructor() {
    this.request = ApiService
  }

  // 质检接口
  // getQualityItems({
  //   projectId,
  //   userId = 0,
  //   limit = 10,
  //   offset = 0,
  //   q = '',
  //   isChecked = '',
  //   filterName = ''
  // }) {
  //   return this.request.get(
  //     `/projects/${projectId}/${userId}/docs?limit=${limit}&offset=${offset}&q=${q}&${filterName}=${isChecked}`
  //   )
  // }
  // 获取大分类保存状态
  getTopLevelLabel(documentId) {
    return this.request.get(
      `/projects/sequenceannotation/getLabel1?documentId=${documentId}`
    )
  }

  // 取得默认页面
  getDefaultPage(userId, projectId) {
    return this.request.get(
      `/label/queryCompletedCount?userId=${userId}&projectId=${projectId}`
    )
  }

  // 2021.8.4 获取复杂大分类标签选中 的时候的接口
  getDocumentListLabels(labelId, documentId) {
    return this.request.get(
      `/projects/sequenceannotation/upload?documentId=${documentId}&labelId=${labelId}&userId=${localStorage.getItem('userId')}`
    )
  }

  getDocumentList({
    projectId,
    userId = 0,
    limit = 10,
    offset = 0,
    q = '',
    isChecked = '',
    filterName = ''
  }) {
    const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
    projectId = sessionStorage.getItem('projectId')
    const page = checkpoint[projectId] ? checkpoint[projectId] : 1
    let tempPage = page != null ? page.page : 1
    // 2021.9.2 未完成状态下标注情况
    // if (localStorage.getItem('notDone')) {
    //   tempPage = 1
    // } else {
    if (tempPage == null) {
      tempPage = localStorage.getItem('currentPage')
    }
    tempPage = tempPage - 1
    // }
    return this.request.get(
      `/projects/${projectId}/${sessionStorage.getItem('currentUserId') ? sessionStorage.getItem('currentUserId') : 0}/docs?limit=${limit}&offset=${offset}&q=${q}&${filterName}=${isChecked}&userId=${userId}&sessionCurrentPage=${tempPage}`
    )
  }

  // 质检

  getQualityItems({
    projectId,
    q = '',
    limit = 10,
    offset = 0,
    startTime = '',
    endTime = '',
    qualityStatus = 0
  }) {
    return this.request.get(
      `/projects/getCancelOperationList?projectId=${projectId}&q=${q}&limit=${limit}&offset=${offset}&startTime=${startTime}&endTime=${endTime}&qualityStatus=${qualityStatus}`
    )
  }
  // 质检驳回

  qualityRollBack(payload) {
    return this.request.get(`/projects/qualityOperation?projectId=${payload.projectId}&documentId=${payload.documentId}&userId=${payload.userId}&opType=${payload.opType}`)
  }

  // 质检撤销
  deleteQuality(projectId, documentId) {
    return this.request.get(`/projects/cancelOperation?projectId=${projectId}&documentId=${documentId}`)
  }

  addDocument(projectId, payload) {
    return this.request.post(`/projects/${projectId}/docs`, payload)
  }

  deleteDocument(projectId, docId) {
    return this.request.delete(`/projects/${projectId}/docs/${docId}`)
  }

  updateDocument(projectId, docId, payload) {
    return this.request.patch(`/projects/${projectId}/docs/${docId}`, payload)
  }

  uploadFile(projectId, payload, config = {}) {
    return this.request.post(
      `/projects/${projectId}/docs/upload`,
      payload,
      config
    )
  }

  exportFile(projectId, format, startTime, endTime) {
    const headers = {}
    if (format === 'csv') {
      headers.Accept = 'text/csv; charset=utf-8'
      headers['Content-Type'] = 'text/csv; charset=utf-8'
    } else {
      headers.Accept = 'application/json'
      headers['Content-Type'] = 'application/json'
    }
    const config = {
      responseType: 'blob',
      params: {
        q: format,
        startTime,
        endTime
      },
      headers
    }
    return this.request.get(`/projects/${projectId}/docs/download`, config)
  }

  approveDocument(projectId, docId, payload) {
    return this.request.post(
      `/projects/${projectId}/docs/${docId}/approve-labels`,
      payload
    )
  }
}

export default new DocumentService()
